#include "gamephasecontrol.h"
#include "gametable.h"
#include <QTimer>
#include <QPainter>
#include <QFinalState>
#include "gamewindow.h"
#include "gamedialogmanager.h"
#include "cutscene.h"



//TEXT DISPLAY ITEM

PhaseConsoleItem::PhaseConsoleItem(GameBoardOrganizer *b)
    :GameBoardItem(b), _content("test")
{

}

QRectF PhaseConsoleItem::boundingRect() const{
    return QRectF(-WIDTH/2, -HEIGHT/2, WIDTH, HEIGHT);
}

void PhaseConsoleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::white);
    painter->drawText(boundingRect(), Qt::AlignCenter, _content);
}

void PhaseConsoleItem::setContent(QString c){
    _content = c;
    update();
}


//organizer


GamePhaseControl::GamePhaseControl(GameTable* t)
    :GameBoardOrganizer(t)
{
    createGameMachine();
    console = new PhaseConsoleItem(this);
    machine->start();

}


/****
See state details in
https://docs.google.com/drawings/d/1uhTmtQukRy32ol64vrPiylCG8eSkUwPvAYvS2y06sL8/edit?hl=en_US
****/
void GamePhaseControl::createGameMachine(){
    QState* root       = new QState;
    machine            = new QStateMachine(getGameTable());
    QState* turn_start = new QState(root);
    QState* subroot    = new QState(root);
    QState* ghost_act  = new QState(subroot);
    QState* ghost_arr  = new QState(subroot);
    QState* tao_act    = new QState(subroot);
    QState* exo_act    = new QState(subroot);
    QState* turn_fin   = new QState(root);
    QFinalState * fin  = new QFinalState(root);

    turn_start->assignProperty(this, "phase", TURN_START);
    turn_start->addTransition(this, SIGNAL(TURN_START_PAUSE()), ghost_arr);
    ghost_act->assignProperty(this, "phase", GHOST_ACT);
    ghost_act->addTransition(this, SIGNAL(TURN_START_PAUSE()), ghost_arr);

    ghost_arr->assignProperty(this, "phase", GHOST_ARR);
    ghost_arr->addTransition(this, SIGNAL(NEXT_PHASE()), tao_act);

    tao_act->assignProperty(this, "phase", TAO_ACT);
    tao_act->addTransition(this->getGameTable(), SIGNAL(exo_succ()), exo_act);

    exo_act->assignProperty(this, "phase", EXO_ACT);

    turn_fin->assignProperty(this, "phase", TURN_FIN);
    turn_fin->addTransition(this, SIGNAL(TURN_START_PAUSE()), turn_start);

    subroot->addTransition(this->getGameTable(), SIGNAL(turn_finished()), turn_fin);
    subroot->addTransition(this->getGameTable(), SIGNAL(game_ended()), fin);

    machine->addState(root);
    machine->setInitialState(root);
    root->setInitialState(turn_start);
    subroot->setInitialState(ghost_act);
}

void GamePhaseControl::setPhase(GAME_PHASE p){
    _phase = p;
    switch(_phase){
    case TURN_START:
        QTimer::singleShot(1000, this, SIGNAL(TURN_START_PAUSE()));
        console->setContent("Turn Start");
        break;
    case GHOST_ACT:
        //QTimer::singleShot(1000, this, SIGNAL(TURN_START_PAUSE()));
        console->setContent("Ghost Action");
        break;
    case GHOST_ARR:
        console->setContent("Ghost Arrival");
        getGameTable()->ghost_arrival();        
        emit NEXT_PHASE();
        break;
    case TAO_ACT:
        console->setContent("Taoist Action");
        GhostingGraphicsObject * ghosting;
        ghosting = CutSceneManager::getGhosting();
        ghosting->setDuration( 1000, 500 );
        ghosting->setPixmap();
        ghosting->animate();
        getGameTable()->taoist_act();
        break;
    case TURN_FIN:
        break;
    }
}

